﻿using GalaSoft.MvvmLight.Command;
using HandyControl.Controls;
using KINLO.Model.MES;
using KINLO.UI.Main.Hepler;
using KINLO.UI.Main.MES;
using KINLO.WebApi.MESModels;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace KINLO.UI.Main.VM
{
    public class EquipmentStatuVM : INotifyPropertyChanged
    {
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void NotifyPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public List<string> ResultComboxs { get; set; } = new List<string>
        {
          "失败"
            // 添加更多选项
        };
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }

        public ObservableCollection<MESEquipmentStatuLogDataModel> DataList { get; set; } = new ObservableCollection<MESEquipmentStatuLogDataModel>();
        public MESEquipmentStatuLogData EquipmentStatuData { get; set; }
        public EquipmentStatuVM()
        {

            EndTime = DateTime.Now.AddDays(+1);
            StartTime = EndTime.AddDays(-30);
            EquipmentStatuData = new MESEquipmentStatuLogData();
            Query();
        }
        RelayCommand<object> cmdEdit;
        public RelayCommand<object> CmdEdit
        {
            get { return cmdEdit ?? (cmdEdit = new RelayCommand<object>((e) => DoCmdEdit(e))); }
        }

        private MESProduction_DayDataLogModel editedItem;
        public MESProduction_DayDataLogModel EditedItem
        {
            get { return editedItem; }
            set
            {
                editedItem = value;
                NotifyPropertyChanged(nameof(EditedItem));
            }
        }
        public void DoCmdEdit(object parameter)
        {
            try
            {
                var row = (MESEquipmentStatuLogDataModel)parameter;
                row.data.ForEach(item =>
                {
                    var begTime = DateTime.Parse(item.Beg_time).ToString("yyyy-MM-dd HH:mm:ss");
                    var endTime = string.IsNullOrWhiteSpace(item.End_time) ? " " : DateTime.Parse(item.End_time).ToString("yyyy-MM-dd HH:mm:ss");
                    item.Beg_time = begTime;
                    item.End_time = endTime;
                });

                int result = InjectMes.MESEquipmentStatus(row.data);
                if (result == 1)
                {
                    EquipmentStatuData.DataList.Where(d => d.ID == row.ID).ToList().ForEach(x =>
                    {


                        x.CurrentStatus = 1;
                        x.SupplementalDate = DateTime.Now;
                    });
                    MessageBox.Success($"补录成功");
                    CFGLoader.Save(EquipmentStatuData);
                    Query();
                }
                else
                {
                    MessageBox.Error($"补录失败！！！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Error($"补录失败,{ex.Message}");
            }

        }

        RelayCommand<object> cmdBatchEdit;
        public RelayCommand<object> CmdCmdBatchEditEdit
        {
            get { return cmdBatchEdit ?? (cmdBatchEdit = new RelayCommand<object>((e) => DoCmdEdit(e))); }
        }

        public void BatchEdit()
        {
            try
            {
                int successNum = 0;
                int errorNum = 0;
                foreach (var row in EquipmentStatuData.DataList)
                {
                    int result = InjectMes.MESEquipmentStatus(row.data);
                    //int result = 1;
                    if (result == 1)
                    {
                        EquipmentStatuData.DataList.Where(d => d.ID == row.ID && d.CurrentStatus != 1).ToList().ForEach(x =>
                        {
                            x.data.ForEach(item =>
                            {
                                var begTime = DateTime.Parse(item.Beg_time).ToString("yyyy-MM-dd HH:mm:ss");
                                var endTime = DateTime.Parse(item.End_time).ToString("yyyy-MM-dd HH:mm:ss");
                                item.Beg_time = begTime;
                                item.End_time = endTime;
                            });
                            x.CurrentStatus = 1;
                            x.SupplementalDate = DateTime.Now;
                            successNum++;
                        });

                    }
                    else
                    {
                        errorNum++;

                    }
                }
                MessageBox.Info($"补录成功数量：{successNum}; 补录失败数量：{errorNum}");
                CFGLoader.Save(EquipmentStatuData);
                Query();

            }
            catch (Exception ex)
            {
                MessageBox.Error($"补录失败,{ex.Message}");
            }

        }

        RelayCommand cmdQuery;
        public RelayCommand CmdQuery
        {
            get { return cmdQuery ?? (cmdQuery = new RelayCommand(() => Query())); }
        }

        public void Query()
        {
            try
            {
                EquipmentStatuData.Load();
                var filteredData = EquipmentStatuData.DataList
                    .Where(item => item.UploadedDate >= StartTime && item.UploadedDate <= EndTime && item.CurrentStatus != 1)
                    .ToList();
                DataList.Clear();
                filteredData.ForEach(item =>
                {
                    DataList.Add(item);
                });
                if (DataList == null || DataList.Count <= 0)
                {
                    //MessageBox.Show("查询结果为空");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Error($"查询失败！！！");
            }
        }
    }
}
